package com.xidian.zhiancloud.service.user;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.dao.syset.WorkFlowUserMapper;
import com.xidian.zhiancloud.model.syset.WorkFlowUserList;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class WorkFlowUserService extends ServiceImpl<WorkFlowUserMapper, WorkFlowUserList> {

    @Autowired
    WorkFlowUserMapper workFlowUserMapper;

    @Autowired
    SysUserMapper usermgMapper;

    public int AddWorkFlowUserList
            (Long companyId,String workflowstepId,String bpmlId,
             String userId,String jobId){
        return workFlowUserMapper.AddWorkFlowUserList
                (companyId,workflowstepId,bpmlId,userId,jobId);
    }

    public List<WorkFlowUserList> getWorkFlowUserList
            (Long companyId,String workflowstepId,String bpmlId){
        return workFlowUserMapper.getWorkFlowUserList(companyId,workflowstepId,bpmlId);
    }

    public List<WorkFlowUserList> selectWorkFlowUser
            (Long companyId,String workflowstepId,String bpmlId){
        return workFlowUserMapper.getWorkFlowUserList(companyId,workflowstepId,bpmlId);
    }
    
    public List<WorkFlowUserList> getByFlChIdAndFlTyAndUsType
        (Long companyId,Integer flowType,Integer flowChildId,String userType){
        return workFlowUserMapper.getByFlChIdAndFlTyAndUsType
            (companyId,flowType,flowChildId,userType);
    }

    public int updateWorkFlowUser(Map<String,Object> params){
        return workFlowUserMapper.updateWorkFlowUser(params);
    }

    public int addWorkFlowUser(Map<String,Object> params){
        return workFlowUserMapper.addWorkFlowUser(params);
    }
    
    public List<WorkFlowUserList> getByFlowTypeAndUser
        (Long companyId,Integer flowType){
        return workFlowUserMapper.getByFlowTypeAndUser(companyId,flowType);
    }

    /**
     * id放入list
     * @param id id(多个已逗号分隔)
     * @return List集合
     */
    public List<String> getList(String userType) {
        List<String> list = new ArrayList<String>();
        String[] str = userType.split(",");
        for (int i = 0; i < str.length; i++) {
            list.add(str[i]);
        }
        return list;
    }
    
    public List<WorkFlowUserList> getUserPersoList(Long companyId,String str) {
        List<WorkFlowUserList> woFlUs= workFlowUserMapper.getUserPersoListsss
            (companyId,str);
        return woFlUs;
    }
    
    //获取事故审核人列表
    public List<Long> getAccidentAudit(Long companyId){
        WorkFlowUserList woFlUs= workFlowUserMapper.getByAccidentAuditAndJobId(companyId);
        String jobIds = String.valueOf(woFlUs.getJobId());
        Integer woFlUsId = woFlUs.getUserId();
        List<Long> userID= new ArrayList<>();
        if(StringUtils.isNotBlank(jobIds)){
            userID = usermgMapper.getByJobIdAndUserID(companyId,jobIds);
        }
        if(woFlUsId==null){
            woFlUsId=0;
        }
        userID.add(woFlUsId.longValue());
        return userID;
    }

    //获取事故调查人列表
    public List<Long> getAccidentSurvey(Long companyId){
        WorkFlowUserList woFlUs= workFlowUserMapper.getByAccidentSurveyAndJobId(companyId);
        String jobIds = String.valueOf(woFlUs.getJobId());
        Integer woFlUsId = woFlUs.getUserId();
        List<Long> userID= new ArrayList<>();
        if(StringUtils.isNotBlank(jobIds)) {
            userID = usermgMapper.getByJobIdAndUserID(companyId,jobIds);
        }
        if(woFlUsId==null){
            woFlUsId=0;
        }
        userID.add(woFlUsId.longValue());
        return userID;
    }

    //获取危化品领用审批人列表
    public List<Long> getByChemicalReviewerAndJobId(Long companyId){
        WorkFlowUserList woFlUs= workFlowUserMapper.getByChemicalReviewerAndJobId(companyId);
        String jobIds = String.valueOf(woFlUs.getJobId());
        Integer woFlUsId = woFlUs.getUserId();
        List<Long> userID= new ArrayList<>();
        if(StringUtils.isNotBlank(jobIds)) {
            userID = usermgMapper.getByJobIdAndUserID(companyId,jobIds);
        }
        if(woFlUsId==null){
            woFlUsId=0;
        }
        userID.add(woFlUsId.longValue());
        return userID;
    }

}
